#include"../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>
#include <google/protobuf/map.h>  
#include <unordered_map>  
#include <string>  
mq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment {
    public:
        virtual void SetUp() override {
            emp = std::make_shared<mq::ExchangeManager>("./data/meta.db");
        }
        virtual void TearDown() override {
            //emp->clear();
            std::cout << "最后的清理！！\n";
        }
};

void ConvertUnorderedMapToProtobufMap(  
    const std::unordered_map<std::string, std::string>& src,  
    google::protobuf::Map<std::string, std::string>* dest) {  
    dest->clear();  // 清空目标映射（如果需要的话）  
    for (const auto& pair : src) {  
        (*dest)[pair.first] = pair.second;  
    }  
}
TEST(exchange_test,insert_test)
{
    std::unordered_map<std::string, std::string> unorderedMap;  
    google::protobuf::Map<std::string, std::string> protobufMap;  
  
    // 转换unorderedMap到protobufMap  
    ConvertUnorderedMapToProtobufMap(unorderedMap, &protobufMap);  
    emp->declareExchange("exchange1", mq::ExchangeType::DIRECT, true, false, protobufMap);
    emp->declareExchange("exchange2", mq::ExchangeType::DIRECT, true, false, protobufMap);
    emp->declareExchange("exchange3", mq::ExchangeType::DIRECT, true, false, protobufMap);
    emp->declareExchange("exchange4", mq::ExchangeType::DIRECT, true, false, protobufMap);
    ASSERT_EQ(emp->size(), 4);
}

TEST(exchange_test, select_test) {
    ASSERT_EQ(emp->exists("exchange1"), true);
    ASSERT_EQ(emp->exists("exchange2"), false);
    ASSERT_EQ(emp->exists("exchange3"), true);
    ASSERT_EQ(emp->exists("exchange4"), true);
    mq::Exchange::ptr exp = emp->selectExchange("exchange3");
    ASSERT_NE(exp.get(), nullptr);
    ASSERT_EQ(exp->name, "exchange3");
    ASSERT_EQ(exp->durable, true);
    ASSERT_EQ(exp->auto_delete, false);
    ASSERT_EQ(exp->type, mq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->getArgs(), std::string("k1=v1&k2=v2&"));
}

// TEST(exchange_test,select_test)
// {
//     mq::Exchange::ptr exp = emp->selectExchange("exchange3");
//     ASSERT_EQ(exp->name,"exchange3");
//     ASSERT_EQ(exp->durable,true);
//     ASSERT_EQ(exp->auto_delete,false);
//     ASSERT_EQ(exp->type,mq::ExchangeType::DIRECT);
//     ASSERT_EQ(exp->getArgs(), std::string("k1=v1&k2=v2&"));
// }

TEST(exchange_test, remove_test) {
    emp->deleteExchange("exchange3");
    mq::Exchange::ptr exp = emp->selectExchange("exchange3");
    ASSERT_EQ(exp.get(), nullptr);
    ASSERT_EQ(emp->exists("exchange3"), false);
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}